package com.example.testdb2.controller;


import com.example.testdb2.entity.UserInfo;
import com.example.testdb2.entity.dto.UserInfoPageDto;
import com.example.testdb2.entity.vo.UserInfoVo;
import com.example.testdb2.mapper.UserMapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("/userinfo")
public class UserController {

    @Autowired
    private UserMapper userMapper;


    @PostMapping("/add")
    public String add(@RequestBody UserInfo userInfo) {

        // String now = LocalDateTime.now().format(UserInfo.formatter);
        LocalDateTime now = LocalDateTime.now();
        userInfo.setCreateTime(now);
        userMapper.add(userInfo);
        return "添加成功,添加的用户sid是" + userInfo.getId();
    }

    @GetMapping("/list")
    public List<UserInfo> list() {

        return userMapper.list();
    }

    @PutMapping("/update")
    public String update(@RequestBody UserInfo userInfo) {

        userInfo.setCreateTime(LocalDateTime.now());
        userMapper.updateById(userInfo);
        return "修改成功";
    }

    @DeleteMapping("/delete/{id}")
    public String delete(@PathVariable Long id) {
        userMapper.deleteById(id);
        return "删除成功";
    }

    @GetMapping("/page")
    public UserInfoVo pageList(UserInfoPageDto userInfoPageDto) {
        PageHelper.startPage(userInfoPageDto.getPageNum(), userInfoPageDto.getPageSize());
        Page page = userMapper.pageList(userInfoPageDto);

        return new UserInfoVo(page.getTotal(),page.getResult());
    }

}
